package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.FocusUser;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FocusUserService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class VideoManager {
    @DubboReference
    private VideoService videoService;

    @DubboReference
    private UserInfoService userInfoService;
//查看推荐视频
    @Cacheable(value = "tanhua", key = "'video_'+#localUserId+'_'+#page+'_'+#pagesize")
    public PageBeanVo smallVideos(Long localUserId, Integer page, Integer pagesize) {
        localUserId = UserHolder.getLocalUserId();
        PageBeanVo pageBeanVo = videoService.findRecommendVideoByPage(localUserId, page, pagesize);
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                Long userId = video.getUserId();
                UserInfo userInfo = userInfoService.findByUserId(userId);
                VideoVo vo = new VideoVo();
                vo.setUserInfo(userInfo);
                vo.setVideo(video);
                voList.add(vo);
                String key = StrUtil.format(ConstantUtil.VIDEO_LIKE, localUserId, video.getUserId());
                if (stringRedisTemplate.hasKey(key)) {
                    vo.setHasFocus(1);
                }
            }
        }
        pageBeanVo.setItems(voList);

        return pageBeanVo;
    }

    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired //java的客户端  报错不管 编译报错
    private FastFileStorageClient client;
//保存视频
    @CacheEvict(value = "tanhua",allEntries = true)
    public ResponseEntity publishVideo(Long localUserId, MultipartFile videoThumbnail
            , MultipartFile videoFile) throws IOException {

        String picPath = "";
        if (videoThumbnail.getSize() > 0 && videoThumbnail != null) {
            picPath = ossTemplate.upload(videoThumbnail.getOriginalFilename(), videoThumbnail.getInputStream());
        }
        String videoPath = "";
        if (videoFile.getSize() > 0 && videoFile != null) {
            StorePath storePath = client.uploadFile
                    (videoFile.getInputStream(),
                            videoFile.getSize(),
                            FileUtil.extName(videoFile.getOriginalFilename()),
                            null);
            videoPath = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        }
        Video video = new Video();
        video.setCreated(System.currentTimeMillis());
        video.setPicUrl(picPath);
        video.setVideoUrl(videoPath);
        video.setUserId(localUserId);
        video.setText("ID--" + localUserId + "-发布的视频");


        videoService.publishVideo(video);


        return ResponseEntity.ok(null);
    }

    @DubboReference
    private FocusUserService focusUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseEntity userFocus(Long focusUserId) {

        Long localUserId = UserHolder.getLocalUserId();

        FocusUser focusUser = new FocusUser();
        focusUser.setUserId(localUserId);
        focusUser.setFocusUserId(focusUserId);
        focusUserService.save(focusUser);
        //放入缓存
        String key = StrUtil.format(ConstantUtil.VIDEO_LIKE, localUserId, focusUserId);
        stringRedisTemplate.opsForValue().set(key, key);
        return null;
    }

    public ResponseEntity userUnFocus(Long focusUserId) {
        Long localUserId = UserHolder.getLocalUserId();
        focusUserService.remove(localUserId, focusUserId);
        String key = StrUtil.format(ConstantUtil.VIDEO_LIKE, localUserId, focusUserId);

        stringRedisTemplate.delete(key);
        return null;
    }
}
